package com.shop.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.pojo.Auction;
import com.shop.pojo.AuctionCustomer;
import com.shop.pojo.Auctionrecord;
import com.shop.pojo.User;
import com.shop.service.AuctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
public class AuctionController {
    //设置 每页显示5条
    private static final int pageSize = 5;
    @Autowired
    private AuctionService auctionService;
    //查询商品拍卖信息
    @RequestMapping(value = "/findAuctionList")
    public String findAuctionList(@ModelAttribute("condition") Auction auction,
                                  BindingResult bindingResult,
                                  Model model,
                                  @RequestParam(name = "pageNum",required = false,defaultValue = "1") int pageNum){
       //进行分页
        PageHelper.startPage(pageNum,pageSize);
        List<Auction> auctionList = auctionService.findAuctionList(auction);
        PageInfo page = new PageInfo(auctionList);
//        //上一页
//        page.getPrePage();
//        //下一页
//        page.getNextPage();
//        //总记录数
//        page.getTotal();
//        //总页码数（尾数）
//        page.getPages();
        //每页记录数
//        page.getPageSize();
//        //当前页码数
//        page.getPageNum();
        model.addAttribute("page",page);
        model.addAttribute("auctionList",auctionList);
        return "index";
    }
    @RequestMapping(value = "/gentAution")
    public String gentAution(String auctionId,Model model){
        Auction auction = this.auctionService.selectAuctionAndRecorList(auctionId);
        model.addAttribute("auctionDetail",auction);
        return "auctionDetail";
    }

//    实现竞拍功能
    @RequestMapping(value = "/saveAuctionRecord")
    public String saveAuctionRecord(Auctionrecord auctionrecord,
                                    HttpSession session) throws Exception {
        User user = (User) session.getAttribute("user");

        //封装userid
        auctionrecord.setUserid(user.getUserid());
        //封装时间
        auctionrecord.setAuctiontime(new Date());
        this.auctionService.saveAuctionRecord(auctionrecord);
        return "redirect:/gentAution?auctionId="+auctionrecord.getAuctionid();
    }

    //查看拍卖结果
    @RequestMapping(value = "/toAuctionResult")
    public String toAuctionResult(Model model){
        List<AuctionCustomer> endTime = this.auctionService.selectAuctionEndTime();
        List<Auction> noEndTime = this.auctionService.selectAuctionNoEndTime();
        model.addAttribute("endtimeList",endTime);
        model.addAttribute("noendtimeList",noEndTime);
        return "auctionResult";
    }
    @RequestMapping(value = "/addAuction")
    public String addAuctionPage(){
        return "addAuction";
    }

    @RequestMapping(value = "/publishAuctions")
    public String publishAuctions(Auction auction,MultipartFile pic) throws IOException {
        //上传图片
        //实现上传操作
        pic.transferTo(new File("F:\\Day_0603(SpringBoot)\\"+pic.getOriginalFilename()));
        //封装上传图片名称
        auction.setAuctionpic(pic.getOriginalFilename());
        //封装上传图片类型
        auction.setAuctionpictype(pic.getContentType());

        this.auctionService.publishAuctions(auction);
        return "redirect:/findAuctionList";
    }
}
